import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import { ListeningMaterial } from '../models/ListeningMaterial';
import { ListeningRecord } from '../models/ListeningRecord';
import { User } from '../models/User';

// 获取听力材料列表
export const getListeningMaterials = async (req: Request, res: Response) => {
  try {
    const materialRepository = getRepository(ListeningMaterial);
    const materials = await materialRepository.find({
      relations: ['category'],
      order: {
        createdAt: 'DESC'
      }
    });
    res.json(materials);
  } catch (error) {
    res.status(500).json({ message: '获取听力材料失败', error });
  }
};

// 获取单个听力材料
export const getListeningMaterial = async (req: Request, res: Response) => {
  try {
    const materialRepository = getRepository(ListeningMaterial);
    const material = await materialRepository.findOne(req.params.id, {
      relations: ['category']
    });

    if (!material) {
      return res.status(404).json({ message: '听力材料不存在' });
    }

    res.json(material);
  } catch (error) {
    res.status(500).json({ message: '获取听力材料失败', error });
  }
};

// 创建听力材料
export const createListeningMaterial = async (req: Request, res: Response) => {
  try {
    const materialRepository = getRepository(ListeningMaterial);
    const material = materialRepository.create(req.body);
    await materialRepository.save(material);
    res.status(201).json(material);
  } catch (error) {
    res.status(500).json({ message: '创建听力材料失败', error });
  }
};

// 更新听力材料
export const updateListeningMaterial = async (req: Request, res: Response) => {
  try {
    const materialRepository = getRepository(ListeningMaterial);
    const material = await materialRepository.findOne(req.params.id);

    if (!material) {
      return res.status(404).json({ message: '听力材料不存在' });
    }

    materialRepository.merge(material, req.body);
    const result = await materialRepository.save(material);
    res.json(result);
  } catch (error) {
    res.status(500).json({ message: '更新听力材料失败', error });
  }
};

// 删除听力材料
export const deleteListeningMaterial = async (req: Request, res: Response) => {
  try {
    const materialRepository = getRepository(ListeningMaterial);
    const material = await materialRepository.findOne(req.params.id);

    if (!material) {
      return res.status(404).json({ message: '听力材料不存在' });
    }

    await materialRepository.remove(material);
    res.status(204).send();
  } catch (error) {
    res.status(500).json({ message: '删除听力材料失败', error });
  }
};

// 获取用户的听力练习记录
export const getUserListeningRecords = async (req: Request, res: Response) => {
  try {
    const recordRepository = getRepository(ListeningRecord);
    const records = await recordRepository.find({
      where: { user: { id: req.params.userId } },
      relations: ['material', 'material.category'],
      order: {
        practiceTime: 'DESC'
      }
    });
    res.json(records);
  } catch (error) {
    res.status(500).json({ message: '获取听力练习记录失败', error });
  }
};

// 创建听力练习记录
export const createListeningRecord = async (req: Request, res: Response) => {
  try {
    const recordRepository = getRepository(ListeningRecord);
    const userRepository = getRepository(User);
    const materialRepository = getRepository(ListeningMaterial);

    const user = await userRepository.findOne(req.body.userId);
    const material = await materialRepository.findOne(req.body.materialId);

    if (!user || !material) {
      return res.status(404).json({ message: '用户或听力材料不存在' });
    }

    const record = recordRepository.create({
      user,
      material,
      score: req.body.score,
      duration: req.body.duration,
      answers: req.body.answers,
      practiceTime: new Date()
    });

    await recordRepository.save(record);
    res.status(201).json(record);
  } catch (error) {
    res.status(500).json({ message: '创建听力练习记录失败', error });
  }
}; 